<?php
ini_set('magic_quotes_gpc','off');
require_once('Zend/Controller/Plugin/Abstract.php');
require_once('Zend/Session/Namespace.php');
require_once('Zend/Session.php');

class Auth extends Zend_Controller_Plugin_Abstract
{
			public $base_url;
			protected $_flashMessenger = null;
			public $dlResources = null;
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {

		global $serverMode,$httpsURLArray,$serverDomainNameURL,$sslServerDomainNameURL;
		
		$ModuleName = $request->getModuleName();
		define('MODULE_NAME',$ModuleName);
		$ControllderName = $request->getControllerName();
		$ActionName = $request->getActionName();
		$id = $request->getParam('id');
		if($_SERVER['HTTP_HOST']=='www.corjanta.com' || $_SERVER['HTTP_HOST']=='corjanta.com')
		{
		header("Location:/coming.html");	exit;
		}
					
		if(MODULE_TYPE == 'admin')
		{
		
			$ModuleName = $request->getModuleName();
				
			if(!isset($_SESSION['COR_ADMIN_ID']) )
			{
			   if($ModuleName!='index')
			   {
				header("Location:/admin-mvc/index/");	exit;
			   }
			}
			else 
			{		
				define('COR_ADMIN_ID', isset($_SESSION['COR_ADMIN_ID'])?$_SESSION['COR_ADMIN_ID']:'');
				 @define('IM_USER_TYPE','');
			}
		}
		if(MODULE_TYPE == 'frontend')
		{
					
			$ModuleName = $request->getModuleName();
			$ControllderName = $request->getControllerName();
			$ActionName = $request->getActionName();
			$formURLCHECK = '/'.$ModuleName.'/'.$ControllderName.'/'.$ActionName;	
			
			$login_notrequired_Urls = array(
									'/survey/index/index',
									'/survey/index/complete'								
								);
			
			if (isset($_SESSION['im_userId']) && $_SESSION['im_userId']!='' ) 
				{      
					 define('COR_USER_ID', isset($_SESSION['im_userId'])?$_SESSION['im_userId']:'');
					 @define('IM_USER_EMAIL',$_SESSION['im_userEmail']);
					 @define('IM_USER_NAME',$_SESSION['im_userName']);
					 @define('IM_USER_TYPE',$_SESSION['im_userType']);
				}
				else
				{
					if(in_array($formURLCHECK, $login_notrequired_Urls) )
				   {
					  header("Location:/home/index/");exit;
				   }
				}
			
		}


    }
	
	function getMyMenu()
 	{
	

 		$usrDetails = Zend_Auth::getInstance()->getIdentity();
		//$usrDetails =$_SESSION['NTRA_GAMES_ADMIN_ID'];
		
    	$userRoles = @($usrDetails->role_ids) ? $usrDetails->role_ids : array('0');
 		
		
    	$admLinks = My_Acl::getInstance()->adminLHSLinks($userRoles);
		
		
		
 		$resourceList = array();
    	foreach($admLinks as $k=>$val)
 		{
 			$resourceList[$k] = array('name'=> $val['name'], 'child'=>$val['items']);
 		}
 		/*
 		//Logged In User Details
    	$usrDetails = Zend_Auth::getInstance()->getIdentity();
    	$usrId = $usrDetails->user_id;

    	$db = Zend_Registry::get ( 'db' ); # DB Object Reference

    	// Get Modules
    	$parentSelect = $db->select()
        			->from(array('a' => 'perm_resource'), array("resource_id","name","parent_id"))
        			->where('a.is_menu_item="N" and a.parent_id="1" and a.status="Y"  ')
        			->order('a.parent_id ASC')
        			->order('a.resource_id ASC');

     	$parentRowset = $db->fetchAll($parentSelect);
     	
 		foreach($parentRowset as $row)
     		$this->parentRes[$row->resource_id] = $row->name;     		
     	
     	// Get Resources
     	$select = $db->select()
        			->from(array('a' => 'perm_user_role'), array())
        			->joinLeft(array('b'=>'perm_role'),'b.role_id=a.role_id', array())
        			->joinLeft(array('c'=>'perm_role_resource'),'c.role_id=b.role_id', array())
        			->joinLeft(array('d'=>'perm_resource'),'c.resource_id=d.resource_id', array("resource_id","name","url","parent_id"))
        			->where('a.user_id = "'.$usrId.'" and b.status="Y" and d.status="Y" and d.is_menu_item="Y" ')
        			->order('d.parent_id ASC')
        			->order('d.resource_id ASC');

     	$rowset = $db->fetchAll($select);

     	if(is_array($rowset) && !empty($rowset)) 
     	{ 
	     	foreach($rowset as $row)
	     	{ 
	     		$this->childRes[$row->resource_id] = get_object_vars($row);
	     		$this->findParent($row->resource_id); # Locate Resource's Module(final Parent)
	     	}
     	}
     	else 
     	{
     		return $this->resourceList; # Skips & Returns Blank Result
     	}

     	// Form Module/MenuItems 
     	foreach($this->parentRes as $key => $row)
     	{
     		$child =  $this->resources[$key];

     		$childs = ""; #Resources
     		if(is_array($child) && !empty($child))
     		{
	     		foreach($child as $crow)
	     		{
		     		if(in_array($crow,array_keys($this->childRes)))
		     		{
			     		$childs[$crow]['url'] = $this->childRes[$crow]['url'];
			     		$childs[$crow]['name']= $this->childRes[$crow]['name'];
		     		}	     		
	     		}
     		}
     		
     		if(!empty($childs) && is_array($child))
     		{
	     		$this->resourceList[$this->inc]['id']	=$key;
	     		$this->resourceList[$this->inc]['name']	=$row;
     			$this->resourceList[$this->inc]['child']= $childs;
	     		$this->inc++;
     		}
     	}
     	*/
    	return $resourceList;	
	}
}

